//http://www.chst.vip:8081/mmb/bijia.html?title=电视&categoryid=0
//http://chst.vip:1234/api/getproductlist?categoryid=0&pageid=1
let fla = true
function fn(categoryid = 0, pageid = 1) {
    $.ajax({
        url: 'http://chst.vip:1234/api/getproductlist',
        data: {
            categoryid: categoryid,
            pageid: pageid,
        }
    }).then(res => {
        console.log(res);
        let html = ''
        res.result.forEach(item => {
            //productQuote productCom productPrice
            // console.log($("#aa")[0].innerText);
            html += `<li class="lis" productId="${item.productId}" categoryId = ${item.categoryId}>
            ${item.productImg}
            <p>
            <span>${item.productName}</span>
            <span>${item.productPrice}</span>
            <span>
            <span> ${item.productQuote}</span>
            <span><span class="iconfont icon-biaoqing"></span>${item.productCom}</span>
            </span>
            </p>
            </li>
            `
        })
        if (fla) {
            fla = false
            let index1 = Math.ceil(res.totalCount / 10)
            let div = document.createElement('div')
            let html2 = ''
            for (let i = 1; i <= index1; i++) {
                html2 += `<option>${i}/${index1}</option>`
            }
            div.className = "leftRight"
            div.innerHTML = `
             <p class="left">上一页</p>
            <div>
            <select class = "pp" autocomplete="off" >
            ${html2}
            </select>
            </div>
            <p class = "right">下一页</p>`

            $('#uu')[0].parentNode.appendChild(div)
            let pID = 1
            $(".pp").on("change", function () {
                pID = $("option:selected", this).val();//需求主键
                pID = pID.substr(0, 1)
                console.log(pID, index1);
                if (pID == 1) {
                    $('.left')[0].style.color = '#ccc'
                } else {
                    $('.left')[0].style.color = '#333'

                }
                if (pID == index1) {
                    console.log('#ccc');
                    $('.right')[0].style.color = '#ccc'
                } else {
                    $('.right')[0].style.color = '#333'
                }
                fn(categoryid, pID)
            });
            $(".left").click(function () {
                $('.right')[0].style.color = '#333'
                if (pID == 1) {
                    // $('.left')[0].style.color = '#ccc'
                    $('.left').attr('disabled', "disabled")
                    alert('第一页')
                } else {
                    pID--
                    $('.left')[0].style.color = '#333'
                    $("option").removeAttr("selected")
                    $("option").eq(pID - 1).prop("selected", true).siblings().attr('selected', false)
                    fn(categoryid, pID)
                    if (pID == 1) {
                        $('.left')[0].style.color = '#ccc'
                    }
                }
            })
            $(".right").click(function () {
                $('.left')[0].style.color = '#333'
                if (pID == index1) {
                    $('.right').attr('disabled', "disabled")
                    alert('最后一页')
                } else {
                    pID++
                    $('.right')[0].style.color = '#333'
                    $("option").removeAttr("selected")
                    $("option").eq(pID - 1).prop("selected", true).siblings().attr('selected', false)
                    fn(categoryid, pID)
                    if (pID == index1) {
                        $('.right')[0].style.color = '#ccc'
                    }
                }
            })
        }

        $('#uu')[0].innerHTML = html
        $('.lis').click(function () {
            let num = this.getAttribute('productId')
            let text1 = this.getAttribute('text1')
            let categoryId = this.getAttribute('categoryId')
            location.href = './productlist.html?' + 'productId='+num + "&" +'text1='+query.title+'&'+'categoryId='+categoryId
            console.log(num);
        })
    })
}


function getUrlParams() {
    let url = location.href;
    let regExp = /.*\?([^\?]+)/
    let query = {};//最终处理好的参数
    let search = regExp.exec(url);
    //productid=23&NAME=123"
    // console.log(search);
    search = search[1];
    //有没有多个query参数,有的话肯定以&进行连接
    let searchArr = [];
    if (search.indexOf("&")) {
        searchArr = search.split("&")
    } else {
        search = search.split()
    }

    searchArr.forEach(item => {
        let queryArr = item.split("=");
        query[queryArr[0]] = queryArr[1]
    })

    return query
}

let query = getUrlParams();
// console.log(query);
query.title = unescape(decodeURI(query.title))//编译中文
// console.log(query);
query.title = query.title.replace(/#/g, '')
console.log(query);
$("#aa")[0].innerHTML = '&nbsp;' + query.title + '>'

$('#aa').prev().click(function(){
    history.back()
})
fn(query.categoryid)

